package 
{
	import away3d.cameras.HoverCamera3D;
    import away3d.containers.View3D;
	import away3d.core.math.Number3D;
    import away3d.materials.ColorMaterial;
    import away3d.primitives.Plane;
    import com.bit101.components.Label;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Vector3D;
    import mge.particles.Particle;
    import mge.particles.ParticleSystem;
    import mge.Timer;
    
	/**
	 * ...
	 * @author ivan
	 */   
	public class Main extends Sprite 
	{
        [Embed(source="../resources/particles.png")]
        private var rParticles:Class;
        
        private var _view:View3D;

		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

        
        private var particles:Vector.<Particle> = new Vector.<Particle>();
        
		private function init(e:Event = null):void 
		{
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
			removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.frameRate = 60;
            
			// entry point
            _view = new View3D();
            addChild(_view);
            
            var material:ColorMaterial = new ColorMaterial(0xFF0000);
            //var plane:Plane = new Plane(material, 100, 100, 1, 1, false);
			var plane:Plane = new Plane();
			plane.x = 50;
			plane.y = 0;
			plane.z = 50;
			plane.material = material;
			plane.width = plane.height = 100;
			plane.yUp = true;
            _view.scene.addChild(plane);
            
			//_view.camera.lens.near = 1;
			//_view.camera.lens.far = 1000;
            _view.camera.position = new Number3D(50, 10, 50);
            
            this.addEventListener(Event.ENTER_FRAME, enterFrame);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
                        
            ps = new ParticleSystem(new rParticles, _view);            
            
            new ParticleEditor(this, ps);
			
			hover = new HoverCamera3D();
            
            label = new Label(this, 0, 0);
            
		}
        private var hover:HoverCamera3D;
        
        private var timer:Timer = new Timer(1000); 
        private var label:Label;
        
        private function enterFrame(e:Event):void
        {
            timer.start();
            ps.update();
            ps.position = new Vector3D(0, 1, 0);
            
            _view.render();
			
			hover.hover(true);
            
            label.text = "UPD: " + (timer.getDelta() * 1000);
        }
                
        private function mouseDown(e:MouseEvent):void
        {
            if (stage.mouseX > 200 && stage.mouseX < 500)
                ps.fire();
        }
        
        private var ps:ParticleSystem;

	}

}